package com.jianyun.jyzs.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.presenter.WarnPresenter;
import com.jianyun.jyzs.utils.LocalSp;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.IWarningView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 预警消息
 * Created by yangshijun on 2018/5/30.
 */

public class WarningActivity extends MvpActivity<IWarningView, WarnPresenter> implements IWarningView {


    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText1)
    Button topText1;
    @BindView(R.id.topText2)
    Button topText2;
    @BindView(R.id.topClander)
    ImageView topClander;
    @BindView(R.id.iv_app_moreLinearLayout)
    LinearLayout ivAppMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.webView)
    WebView webView;

    private int type = 2;
    private String erpRootUrl;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_warning_notice);
        ButterKnife.bind(this);
        erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        initData();
        initView();
    }

    private void initData() {
        LocalSp.getInstance().clearAreaCount(2);
        topBack.setVisibility(View.VISIBLE);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setSupportZoom(true);
        webView.setWebViewClient(new OAWebViewClient());
        webView.getSettings().setDomStorageEnabled(true);
    }

    private void initView() {
        loadUrl(type);
    }

    private void loadUrl(int type) {
        webView.loadUrl(erpRootUrl+"EWManager/MsgList.aspx?employeeid="+userId+"&type="+type);

    }

    @NonNull
    @Override
    public WarnPresenter createPresenter() {
        return new WarnPresenter();
    }

    @OnClick({R.id.topBack, R.id.topText1, R.id.topText2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.topText1:
                topText1.setBackgroundResource(R.drawable.bg_aralm_relevant_false);//bg_aralm_relevant_false
                topText2.setBackgroundResource(R.drawable.bg_aralm_res_true);
                topText1.setTextColor(getResources().getColorStateList(R.color.white));
                topText2.setTextColor(getResources().getColorStateList(R.color.bacck333));
                this.type = 2;
                loadUrl(type);
                break;
            case R.id.topText2:
                topText2.setBackgroundResource(R.drawable.bg_aralm_res_false);//bg_aralm_res_false
                topText1.setBackgroundResource(R.drawable.bg_aralm_relevant_true);
                topText2.setTextColor(getResources().getColorStateList(R.color.white));
                topText1.setTextColor(getResources().getColorStateList(R.color.bacck333));
                this.type = 1;
                loadUrl(type);
                break;
        }
    }

    class OAWebViewClient extends WebViewClient {


        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }
}
